<?php
namespace Hawk\Controller;
use \Hawk\Controller\IndexController;
class PositionController extends IndexController 
{
    /**
     * 股票持仓添加
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function add(){
		if(IS_POST){
			$model=D("Hawk/Position");
			if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
                    $content = "添加(".$_POST['cus_id'].")股票操作记录(".$id.")";
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
    				$this->success('添加成功！', U('Service/lst?p='.I('get.p')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
		}
		if(!empty(I('get.id'))){
			$tmodel=M('StockType');
			$type = $tmodel->select();
			$this->assign('type',$type);
			$this->assign('cus_id',I('get.id'));
			$this->display();
		}
	}
    /**
     * 股票持仓修改
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function edit(){
		$id = I('get.id');
        $model = D('Hawk/Position');
        if(IS_POST && !empty($_POST))
        {
            if($model->create(I('post.'), 2))
            {
                if($model->save() !== FALSE)
                {
                    $content = "修改股票操作记录(".$_POST['id'].")";
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
                    $this->success('修改成功！', U('lst', array('stock_code' => I('post.stock_code'),'cus_id'=>I('post.cus_id'))));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $stockInfo=$model->find($id);
        $this->assign('stockInfo',$stockInfo);
        $tmodel = M('StockType');
		$type = $tmodel->select();
		$this->assign('type',$type);
        $this->display();
	}
    /**
     * 股票持仓删除
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function delete(){
		$model = D('Hawk/Position');
		$stock_self = $model->find(I('get.id'));
		if($model->delete(I('get.id', 0)) !== FALSE)
    	{
            //写入操作日志
            $content = "成功删除客户".$stock_self['cus_id']."-".$stock_self['stock_name']."操作记录";

            hawk_operation_log(session('id'),hawk_get_ip(),$content);

    		$this->success('删除成功！', U('lst', array('stock_code' => $stock_self['stock_code'],'cus_id'=>$stock_self['cus_id'])));
    		exit;
    	}
    	else 
    	{
            //写入操作日志
            $content = "删除客户".$stock_self['cus_id']."-".$stock_self['stock_name']."操作记录失败";

            hawk_operation_log(session('id'),hawk_get_ip(),$content);
            
    		$this->error($model->getError());
    	}	
	}
    /**
     * 股票持仓列表
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function lst(){
		if(!empty(I('get.stock_code'))&&!empty(I('get.cus_id'))){
			$where['stock_code']=I('get.stock_code');
			$where['cus_id']=I('get.cus_id');
			$where['status'] = 0;
			$model=M('Position');
			$list = $model->where($where)->select();
			$this->assign('cus_id',I('get.cus_id'));
			$this->assign('list',$list);
			$this->display();
		}else{
			$this->error('无此权限！');
		}
	}
}